package com.workManager.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.workManager.mapper.TbParameterMapper;
import com.workManager.mapper.TbParameterTypeMapper;
import com.workManager.pojo.*;
import com.workManager.vo.ParameterTypeVo;
import com.workManager.vo.RoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;

/**
 * Created by Thinkpad on 2017/2/24 0024.
 */
@Service
public class ParameterServiceImpl implements ParameterService {
    @Autowired
    private TbParameterMapper parameterMapper;
    @Autowired
    private TbParameterTypeMapper parameterTypeMapper;
    public List<TbParameter> queryByParameterType(String parameterTypeValue) {
        return parameterMapper.queryByParameterType(parameterTypeValue);
    }

    public int insertParameterType(TbParameterType parameterType) {
        return parameterTypeMapper.insert(parameterType);
    }

    public int insertParameter(TbParameter parameter) {
        return parameterMapper.insert(parameter);
    }

    public int deleteParameterType(List<String> typeIds) {


        for (int i = 0;i<typeIds.size();i++){
            parameterMapper.deleteParameterByTypeId(typeIds.get(i));
            parameterTypeMapper.deleteByPrimaryKey(Integer.parseInt(typeIds.get(i)));
        }


      return   1;
    }

    public int deleteParameter(String id) {
        return parameterMapper.deleteByPrimaryKey(Integer.parseInt(id));
    }

    public int deleteParameterByList(List<String> ids) {
        int count = 0;
        for (String id:ids){
            count+=parameterMapper.deleteByPrimaryKey(Integer.parseInt(id));
        }
        return count;
    }


    public PageInfo<ParameterTypeVo> queryParameterType2(Map map) {
        int pageno = !map.containsKey("pagenum")||map.get("pagenum")==null?1:Integer.parseInt(map.get("pagenum").toString());
        int pagesize = !map.containsKey("pagesize")||map.get("pagesize")==null?10:Integer.parseInt(map.get("pagesize").toString());
        PageHelper.startPage(pageno,pagesize);
        List<ParameterTypeVo> parameterTypeList = parameterTypeMapper.queryParameterType2(map);
        PageInfo<ParameterTypeVo> parameterTypePageInfo = new PageInfo<ParameterTypeVo>(parameterTypeList);
        return  parameterTypePageInfo;
    }

    public PageInfo<TbParameterType> queryParameterType(Map map) {
        int pageno = !map.containsKey("pagenum")||map.get("pagenum")==null?1:Integer.parseInt(map.get("pagenum").toString());
        int pagesize = !map.containsKey("pagesize")||map.get("pagesize")==null?10:Integer.parseInt(map.get("pagesize").toString());
        String parameterTypeName = (String) map.get("parameterTypeName");
        PageHelper.startPage(pageno,pagesize);

        TbParameterTypeExample parameterTypeExample = new TbParameterTypeExample();

        if(StringUtils.isNotBlank(parameterTypeName)){
            parameterTypeName = "%" + parameterTypeName + "%";
            parameterTypeExample.or().andIsValidEqualTo(true).andParamTypeNameLike(parameterTypeName);
        }else{
            parameterTypeExample.or().andIsValidEqualTo(true);
        }
        parameterTypeExample.setOrderByClause("CREATE_DATE desc");
        List<TbParameterType> parameterTypeList =  parameterTypeMapper.selectByExample(parameterTypeExample);


        PageInfo<TbParameterType> parameterType = new PageInfo<TbParameterType>(parameterTypeList);
        return  parameterType;

    }

    public int update(TbParameter parameter) {
        return parameterMapper.updateByPrimaryKey(parameter);
    }

    public int updateParameterType(TbParameterType parameterType) {
        return parameterTypeMapper.updateByPrimaryKey(parameterType);
    }

    public PageInfo<TbParameter> queryParameter(Map map) {
        int pageno = !map.containsKey("pagenum")||map.get("pagenum")==null?1:Integer.parseInt(map.get("pagenum").toString());
        int pagesize = !map.containsKey("pagesize")||map.get("pagesize")==null?10:Integer.parseInt(map.get("pagesize").toString());
        PageHelper.startPage(pageno,pagesize);
        List<TbParameter> parameterList =  parameterMapper.queryParameter(map);
        PageInfo<TbParameter> parameterPageInfo = new PageInfo<TbParameter>(parameterList);
        return  parameterPageInfo;
    }



    /**
     * 根据参数类型名称获取子参数类型列表
     * @param paramTypeName
     * @return
     */
    public List<TbParameterType> getChildrenByName(String paramTypeName){
        //获取参数类型ID
        TbParameterTypeExample parameterTypeExample = new TbParameterTypeExample();
        parameterTypeExample.or().andIsValidEqualTo(true).andParamTypeNameEqualTo(paramTypeName);
        List<TbParameterType> parameterTypeList = parameterTypeMapper.selectByExample(parameterTypeExample);
        List<Integer> paramTypeIdList = new ArrayList<Integer>();

        Iterator<TbParameterType> iterator = parameterTypeList.iterator();
        while(iterator.hasNext()){
            TbParameterType parameterType = iterator.next();
            paramTypeIdList.add(parameterType.getParamTypeId());
        }

        //获取子参数类型
        List<TbParameterType> childrenList = new ArrayList<TbParameterType>();
        if(paramTypeIdList.size()> 0){
            parameterTypeExample.clear();
            parameterTypeExample.or().andIsValidEqualTo(true).andParentIdIn(paramTypeIdList);
            childrenList = parameterTypeMapper.selectByExample(parameterTypeExample);
        }

        return childrenList;
    }

    /**
     * 根据参数类型列表获取参数类型ID列表
     * @param parameterTypeList
     * @return
     */
    public List<Integer> getParamTypeIdByList(List<TbParameterType> parameterTypeList){
        List<Integer> paramTypeIdList = new ArrayList<Integer>();

        Iterator<TbParameterType> iterator = parameterTypeList.iterator();
        while(iterator.hasNext()){
            TbParameterType parameterType = iterator.next();
            paramTypeIdList.add(parameterType.getParamTypeId());
        }

        return paramTypeIdList;
    }

    /**
     * 根据参数类型列表获取参数类型名称与ID的键值对
     * @param parameterTypeList
     * @return
     */
    public Map<String, Integer> getNameAndIdMapByParameterTypes(List<TbParameterType> parameterTypeList){
        Map<String, Integer> map = new HashMap<String, Integer>();

        Iterator<TbParameterType> iterator = parameterTypeList.iterator();
        while(iterator.hasNext()){
            TbParameterType parameterType = iterator.next();
            map.put(parameterType.getParamTypeName(), parameterType.getParamTypeId());
        }

        return map;
    }

    /**
     * 根据参数类型ID列表获取参数列表
     * @param paramTypeIdList
     * @return
     */
    public List<TbParameter> getParametersByParamTypeIds(List<Integer> paramTypeIdList){
        List<TbParameter> parameterList = new ArrayList<TbParameter>();

        if(paramTypeIdList.size() > 0){
            TbParameterExample parameterExample = new TbParameterExample();
            parameterExample.or().andIsValidEqualTo(true).andParamTypeIdIn(paramTypeIdList);
            parameterList = parameterMapper.selectByExample(parameterExample);
        }

        return parameterList;
    }

    /**
     * 根据参数列表获取参数名称与ID的键值对
     * @param parameterList
     * @return
     */
    public Map<String, Integer> getNameAndIdMapByParameters(List<TbParameter> parameterList){
        Map<String, Integer> map = new HashMap<String, Integer>();

        Iterator<TbParameter> iterator = parameterList.iterator();
        while(iterator.hasNext()){
            TbParameter parameter = iterator.next();
            map.put(parameter.getParamName(), parameter.getParamId());
        }

        return map;
    }

    /**
     * 根据参数列表获取参数名称列表
     * @param parameterList
     * @return
     */
    public List<String> getParameterNames(List<TbParameter> parameterList){
        List<String> paramNameList = new ArrayList<String>();

        for(TbParameter parameter : parameterList){
            paramNameList.add(parameter.getParamName());
        }

        return paramNameList;
    }

}
